package com.ibeifeng.crm.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/login/")
public class LoginController {
	@RequestMapping("/shiroLogin")
	public String login(HttpServletRequest request, HttpServletResponse response){
		String userCode=request.getParameter("userCode");
		String password=request.getParameter("password");
//		ModelAndView mav=new ModelAndView();
		String str=null;
		Subject subject=SecurityUtils.getSubject();
		UsernamePasswordToken token =new UsernamePasswordToken(userCode,password);
		try {
			subject.login(token);
			System.out.println("*************认证成功*************");
			str="successUrl.do";
		
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("*************认证失败*************");
			str="redirect:/login.jsp";
		}
		return str;
	}
	@RequestMapping("/loginUrl")
	public ModelAndView loginUrl(){
		System.out.println("*************认证登录界面*************");
		return new ModelAndView("login.jsp");
	}
	@RequestMapping("/unauthUrl")
	public ModelAndView unauthUrl(){
		System.out.println("*************认证失败*************");
		return new ModelAndView("test.jsp");
	}
	@RequestMapping("/successUrl")
	public ModelAndView successUrl(String userCode,HttpServletRequest request){
		System.out.println("*************认证成功跳转*************");
		ModelAndView mav=new ModelAndView("redirect:/index.jsp");
//		mav.addObject("userCode", userCode);
		request.getSession().setAttribute("userCode", userCode);
		request.getSession().setAttribute("time", new Date());
		return mav ;
	}
}
